local getmetatable = getmetatable
local setmetatable = setmetatable
local module = module
local require = require
local type = type
local print = print
local tostring = tostring
local tonumber = tonumber
local error = error
local assert = assert
local pairs = pairs

module('fit.snmp.SnmpFixture')
_VERSION='0.2'

local Fixture = require('fit.Fixture')
local SnmpSession = require('fit.snmp.SnmpSession')
local snmp = require('snmp')

-- Debug Functions

local function printTable(t, indentStr)
    for k in pairs(t) do

        if type(t[k]) == "table" then
            print(indentStr .. k .. ":")
            local newIndentStr = indentStr .. " "
            printTable(t[k], newIndentStr)

        elseif type(t[k]) == "function" then
            print(indentStr .. k.. ": function");
        elseif type(t[k]) == "boolean" then
            print(indentStr .. k .. ": " .. tostring(t[k]))
        else
            print(indentStr .. k .. ": " .. t[k])
        end
    end
end

-- Config Functions

local function config_peer(self, peer)
    self.session.config.peer = peer
end

local function config_user(self, user)
    self.session.config.user = user
end

local function config_password(self, password)
    self.session.config.password = password
end

local function config_authType(self, authType)
    self.session.config.authType = authType
end

local function config_securityLevel(self, securityLevel)
    self.session.config.securityLevel = securityLevel
end

local function config_context(self, context)
    self.session.config.context = context
end


-- SNMP Functions

local function snmp_open(self)
    self.session:snmp_open()
end

local function snmp_close(self)
    self.session:snmp_close()
end

local function snmp_get(self, var)
    self.session:snmp_get(var)
end

local function snmp_getnext(self, var)
    self.session:snmp_getnext(var)
end

local function snmp_walk(self, var)
    self.session:snmp_walk(var)
end

local function snmp_set(self)
    self.session:snmp_set()
end

-- Varbind List Attribute Get Functions (operates on the first result only)

local function vlist_type(self, newType)
    return self.session.numtostring(self.session:vb_list()[1].type)
end

local function vlist_value(self, value)
    return tostring(self.session:vb_list()[1].value)
end

local function vlist_oid(self, oid)
    return tostring(self.session:vb_list()[1].oid)
end

local function vlist_tostring(self)
    return tostring(self.session:vb_list()[1])
end

function __call()
    local snmpFixture = Fixture()

    snmpFixture.session = SnmpSession()

    -- Configuration
    snmpFixture.config_peer = config_peer
    snmpFixture.config_user = config_user
    snmpFixture.config_password = config_password
    snmpFixture.config_authType = config_authType
    snmpFixture.config_securityLevel = config_securityLevel
    snmpFixture.config_context = config_context
    snmpFixture.config = {} -- config table
    snmpFixture.config.version = snmp.SNMPv3 -- Default to SNMPv3

    -- SNMP Functions
    snmpFixture.snmp_open = snmp_open
    snmpFixture.snmp_close = snmp_close
    snmpFixture.snmp_get = snmp_get
    snmpFixture.snmp_getnext = snmp_getnext
    snmpFixture.snmp_set = snmp_set
    snmpFixture.snmp_walk = snmp_walk

    -- Varbind List Attribute Get Functions
    snmpFixture.vlist_type = vlist_type
    snmpFixture.vlist_value = vlist_value
    snmpFixture.vlist_oid = vlist_oid
    snmpFixture.vlist_tostring = vlist_tostring

    return snmpFixture
end

setmetatable(_M, _M)
